package com.rahms.server.controller;

import com.rahms.common.constant.CacheConstant;
import com.rahms.common.result.R;
import com.rahms.server.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 缓存管理控制器
 */
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 清除所有缓存
     */
    @PostMapping("/clear-all")
    public R<Boolean> clearAllCache() {
        try {
            // 清除所有缓存
            redisUtil.delete("*");
            return R.success(true).add("message", "所有缓存已清除");
        } catch (Exception e) {
            return R.error("清除缓存失败: " + e.getMessage());
        }
    }

    /**
     * 清除用户相关缓存
     */
    @PostMapping("/clear-user")
    public R<Boolean> clearUserCache(@RequestParam(required = false) Long userId) {
        try {
            if (userId != null) {
                // 清除特定用户缓存
                redisUtil.delete(CacheConstant.USER_INFO_CACHE + userId);
                redisUtil.delete(CacheConstant.USER_LIST_CACHE + "*");
            } else {
                // 清除所有用户缓存
                redisUtil.delete(CacheConstant.USER_CACHE_PREFIX + "*");
            }
            return R.success(true).add("message", "用户缓存已清除");
        } catch (Exception e) {
            return R.error("清除用户缓存失败: " + e.getMessage());
        }
    }

    /**
     * 清除药品相关缓存
     */
    @PostMapping("/clear-medication")
    public R<Boolean> clearMedicationCache() {
        try {
            redisUtil.delete(CacheConstant.MEDICATION_CACHE_PREFIX + "*");
            return R.success(true).add("message", "药品缓存已清除");
        } catch (Exception e) {
            return R.error("清除药品缓存失败: " + e.getMessage());
        }
    }

    /**
     * 清除健康数据相关缓存
     */
    @PostMapping("/clear-health-data")
    public R<Boolean> clearHealthDataCache() {
        try {
            redisUtil.delete(CacheConstant.HEALTH_DATA_CACHE_PREFIX + "*");
            return R.success(true).add("message", "健康数据缓存已清除");
        } catch (Exception e) {
            return R.error("清除健康数据缓存失败: " + e.getMessage());
        }
    }

    /**
     * 获取缓存信息
     */
    @GetMapping("/info")
    public R<Object> getCacheInfo(@RequestParam String key) {
        try {
            Object value = redisUtil.get(key);
            if (value != null) {
                return R.success(value).add("message", "缓存信息获取成功");
            } else {
                return R.error("缓存不存在");
            }
        } catch (Exception e) {
            return R.error("获取缓存信息失败: " + e.getMessage());
        }
    }

    /**
     * 设置缓存
     */
    @PostMapping("/set")
    public R<Boolean> setCache(@RequestParam String key, 
                               @RequestParam String value,
                               @RequestParam(defaultValue = "1800") long timeout) {
        try {
            redisUtil.set(key, value, timeout, java.util.concurrent.TimeUnit.SECONDS);
            return R.success(true).add("message", "缓存设置成功");
        } catch (Exception e) {
            return R.error("设置缓存失败: " + e.getMessage());
        }
    }

    /**
     * 删除缓存
     */
    @PostMapping("/delete")
    public R<Boolean> deleteCache(@RequestParam String key) {
        try {
            boolean result = redisUtil.delete(key);
            return result ? R.success(true).add("message", "缓存删除成功") : R.error("缓存不存在");
        } catch (Exception e) {
            return R.error("删除缓存失败: " + e.getMessage());
        }
    }
}
